import 'dart:io';

import 'package:common_utils/common_utils.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter_udid/flutter_udid.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../config/config.dart';
import '../../routers/routers.dart';
import '../../utils/singleton_manager.dart';

class SplashLogic extends GetxController {
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    LogUtil.v('SplashLogic=onInit');
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    initEasyRefresh();
    getDeviceInfo();
    getAppVersion();
    Future.delayed(const Duration(milliseconds: 2600), () {
      // if(!UserUtils.isLogin()){
      //   Get.offNamed(Routes.login);
      // }
      Get.offNamed(Routes.home);
    });
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  initEasyRefresh() {
    EasyRefresh.defaultHeaderBuilder = () => CupertinoHeader(
          foregroundColor: Config.colorText3.withOpacity(0.8),
        );
    EasyRefresh.defaultFooterBuilder = () => CupertinoFooter(
          foregroundColor: Config.colorText3.withOpacity(0.8),
        );
  }

  getAppVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String appName = packageInfo.appName;
    String packageName = packageInfo.packageName;
    String version = SingletonManager().appVer = packageInfo.version;
    String versionCode = packageInfo.buildNumber;
    final box = GetStorage();
    box.write('app_ver', version);

    LogUtil.v('AppName=$appName');
    LogUtil.v('AppID=$packageName');
    LogUtil.v('AppVerName=$version');
    LogUtil.v('AppVerCode=$versionCode');
  }

  getDeviceInfo() async {
    final box = GetStorage();
    String os = SingletonManager().os = box.read("dev_os") ?? '';
    String osVer = SingletonManager().osVer = box.read("dev_os_ver") ?? '';
    String model = SingletonManager().model = box.read("dev_model") ?? '';
    String brand = SingletonManager().brand = box.read("dev_brand") ?? '';
    String udid = SingletonManager().udid = box.read("dev_udid") ?? '';

    if (os.isNotEmpty && osVer.isNotEmpty && model.isNotEmpty && udid.isNotEmpty) {
      return;
    }

    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      os = SingletonManager().os = 'Android';
      osVer = SingletonManager().osVer = androidInfo.version.release;
      model = SingletonManager().model = androidInfo.model;
      brand = SingletonManager().brand = androidInfo.brand;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      os = SingletonManager().os = 'iOS';
      osVer = SingletonManager().osVer = iosInfo.systemVersion;
      model = SingletonManager().model = iosInfo.utsname.machine; //.name
      brand = SingletonManager().brand = 'apple';
    }
    udid = await FlutterUdid.udid;
    SingletonManager().udid = udid;
    box.write('dev_uuid', udid);
    box.write('dev_os', os);
    box.write('dev_os_ver', osVer);
    box.write('dev_model', model);
    box.write('dev_brand', brand);

    LogUtil.v('设备系统类型=$os');
    LogUtil.v('设备系统版本=$osVer');
    LogUtil.v('设备品牌=$brand');
    LogUtil.v('设备产品型号=$model');
    LogUtil.v('设备UDID=$udid');
  }
}
